from osv import fields, osv
from tools.translate import _

#Add a type in partner address object named "Location"
class res_partner_address(osv.osv):
    _description ='Partner Addresses'
    _name = 'res.partner.address'
    _inherit='res.partner.address'
    _columns={
             'type': fields.selection( [('default','Default'),('location','Location'),('invoice','Invoice'), ('delivery','Delivery'), ('contact','Contact'), ('other','Other') ],'Address Type'),
             }
    
    def name_search(self, cr, user, name, args=None, operator='ilike', context=None, limit=80):
        if not args:
            args=[]
        if not context:
            context={}
        if context.get('contact_display', 'contact')=='partner':
            ids = self.search(cr, user, [('partner_id',operator,name)], limit=limit, context=context)
        else:
            if not name:
                ids = self.search(cr, user, args, limit=limit, context=context)
            else:
                ids = self.search(cr, user, [('zip','=',name)] + args, limit=limit, context=context)
            if not ids:
                ids = self.search(cr, user, [('city',operator,name)] + args, limit=limit, context=context)
            if not ids:
                ids = self.search(cr, user, [('name',operator,name)] + args, limit=limit, context=context)
            if not ids:
                ids = self.search(cr, user, [('street',operator,name)] + args, limit=limit, context=context)
            if not ids:
                ids = self.search(cr, user, [('partner_id',operator,name)] + args, limit=limit, context=context)
        return self.name_get(cr, user, ids, context=context)
    
    def name_get(self, cr, user, ids, context={}):
        if not len(ids):
            return []
        res = []
        for r in self.read(cr, user, ids, ['zip','city','partner_id', 'street']):
            if context.get('contact_display', 'contact')=='partner':
                res.append((r['id'], r['partner_id'][1]))
            else:
                addr = ''
                r_zip = ''
                if r['zip']:
                    r_zip = ", " + r['zip']
                r_city=''
                if r['city']:
                    r_city = ", " + r['city']
                addr += (r['street'] or '') +  (r_zip or '') + (r_city or '')
                res.append((r['id'], addr.strip() or '/'))
        return res
res_partner_address()

class res_partner(osv.osv):
    _description ='Partner'
    _name = 'res.partner'
    _inherit='res.partner'
    
    def name_search(self, cr, user, name='', args=None, operator='ilike', context=None, limit=80):
        if not args:
            args=[]
        if not context:
            context={}
        if name:
            context['search_field'] = 'ref'
            context['operator'] = '='
            ids = self.search(cr, user, [('ref','=',name)]+ args, limit=limit, context=context)
            if not len(ids):
                context['operator'] = operator
                ids = self.search(cr, user, [('ref',operator,name)]+ args, limit=limit, context=context)
            if not len(ids):
                context['search_field'] = 'name'
                context['operator'] = '='
                ids = self.search(cr, user, [('name','=',name)]+ args, limit=limit, context=context)
            if not len(ids):
                context['operator'] = operator
                ids = self.search(cr, user, [('name',operator,name)]+ args, limit=limit, context=context)
            if not len(ids):
                context['search_field'] = 'trade_name'
                context['operator'] = '='
                ids = self.search(cr, user, [('trade_name','=',name)]+ args, limit=limit, context=context)
            if not len(ids):
                context['operator'] = operator
                ids = self.search(cr, user, [('trade_name',operator,name)]+ args, limit=limit, context=context)
        else:
            ids = self.search(cr, user, args, limit=limit, context=context)
            
        result = self.name_get(cr, user, ids, context=context)
        if 'return_context' in context and ids:
            if 'search_field' in context:
                result.append({'search_field':context['search_field'],'operator':context['operator']})
        return result

res_partner()
